package com.manage.platform.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.platform.dto.TaskAchieveFileDto;
import com.manage.platform.entity.TaskAchieveEntity;
import com.manage.platform.entity.TaskAchieveFileEntity;
import com.manage.platform.mapper.TaskAchieveFileMapper;
import com.manage.platform.mapper.TaskAchieveMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * YWD
 * 2025/11/24
 */
@AllArgsConstructor
@Service
public class TaskAchieveFileServiceImpl extends ServiceImpl<TaskAchieveFileMapper, TaskAchieveFileEntity> {

    private final TaskAchieveFileLinkServiceImpl taskAchieveFileLinkServiceImpl;


    public Boolean saveTaskAchieve(Long taskAchieveId, List<TaskAchieveFileDto> achieveFileVoList) {
        achieveFileVoList.forEach(it -> {
            it.setTaskAchieveId(taskAchieveId);
            this.baseMapper.saveTaskAchieve(it);
            if (CollectionUtil.isNotEmpty(it.getAchieveFileLinkVoList())) {
                taskAchieveFileLinkServiceImpl.saveTaskAchieve(it.getId(), it.getAchieveFileLinkVoList());
            }
        });
        return true;
    }


    public Boolean updateTaskAchieve(Long taskAchieveId, List<TaskAchieveFileDto> achieveFileVoList) {
        achieveFileVoList.forEach(it -> {
            it.setTaskAchieveId(taskAchieveId);
            this.baseMapper.saveTaskAchieve(it);
            if (CollectionUtil.isNotEmpty(it.getAchieveFileLinkVoList())) {
                taskAchieveFileLinkServiceImpl.updateTaskAchieve(it.getId(), it.getAchieveFileLinkVoList());
            }
        });
       return true;
    }


    public Boolean removeByTaskAchieveId(Long taskAchieveId) {
        LambdaQueryWrapper<TaskAchieveFileEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskAchieveFileEntity::getTaskAchieveId, taskAchieveId);
        return this.remove(queryWrapper);
    }


    public Boolean removeByTaskAchieveId(List<Long> taskAchieveIds) {
        LambdaQueryWrapper<TaskAchieveFileEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TaskAchieveFileEntity::getTaskAchieveId, taskAchieveIds);
        return this.remove(queryWrapper);
    }


    public Boolean deleteTaskAchieve(Long taskAchieveId) {
        LambdaQueryWrapper<TaskAchieveFileEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskAchieveFileEntity::getTaskAchieveId, taskAchieveId);
        List<Long> list = list(queryWrapper).stream().map(TaskAchieveFileEntity::getId).toList();
        taskAchieveFileLinkServiceImpl.deleteTaskAchieve(list);
        return this.removeById(taskAchieveId);
    }


    public List<Long> getIdByTaskAchieveId(Long taskAchieveId) {
        LambdaQueryWrapper<TaskAchieveFileEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskAchieveFileEntity::getTaskAchieveId, taskAchieveId);
        return list(queryWrapper).stream().map(TaskAchieveFileEntity::getId).toList();
    }


}
